package commandAction;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import robotCross.TextMatcher;
import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.tester.JTreeLocation;
import abbot.tester.JTreeTester;
import abbot.tester.Robot;
import com.supermap.desktop.ui.UICommonToolkit;
import com.supermap.desktop.ui.controls.LayersTree;
import command.Command;
import command.SearchMap;

public class ActionLayerTree extends Action {
	JTree tree = null;
	TreePath treePath;
	ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>();
	LayersTree layersTree = UICommonToolkit.getLayersManager().getLayersTree();
	
	@Override
	public Object action(Command cmd) {
		 JTreeTester treeTester = new JTreeTester();
		 treeNodes = findLayerTreeNodes(layersTree, cmd.searchMap);
			for(int i=0;i<treeNodes.size();i++){
				if (cmd.searchMap== null || cmd.searchMap.size()== 0){
					break;	
				}
				treePath =new TreePath(((DefaultMutableTreeNode) treeNodes.get(i)).getPath());
				treeTester.actionSelectPath(layersTree, treePath);
			}
			if (cmd.getMenuItem()!=null)
			{  
				Robot.delay(3000);
			    treeTester.actionSelectPopupMenuItem(layersTree, new JTreeLocation(treePath),cmd.getMenuItem());
			    }
			return null;
   }	
   ArrayList<TreeNode> findLayerTreeNodes(JTree tree,ArrayList<SearchMap> layerList) { 
	this.tree = tree;
	TreeNode root = (TreeNode) tree.getModel().getRoot();
	treeNodes.clear();
	treeNodes.add(root);
	if (layerList == null || layerList.size() == 0) {
		return treeNodes;
	}
	Iterator<SearchMap> iterator = layerList.iterator();
	while (iterator.hasNext()) {
		    SearchMap map = iterator.next();
		    if (treeNodes.size() == 0)
		    {   
		    	System.out.println("父节点没有找到时，直接返回结果，不用再遍历子节点!");
				return treeNodes;
				}
			visitAllNodes(treeNodes.get(0), map);
	}
	  return treeNodes;
}
   public void visitAllNodes(TreeNode treeNode, SearchMap map) {
		   ArrayList<TreeNode> tmpNodes = new ArrayList<TreeNode>();
		   // 按照名称查找所有节点
			String nameRegexp = ((String) map.getValue()).replace(',', '|');
			//忽略前后空格
			nameRegexp = "/\\s{0,}"+nameRegexp+"\\s{0,}/";
			for (int i = 0; i < treeNode.getChildCount(); i++) {
				JComponent nodeCmp = (JComponent) tree.getCellRenderer()
						.getTreeCellRendererComponent(tree,
								treeNode.getChildAt(i), false, false, false, 0,
								false);
				try {
					//使用标题和container里面的控件进行匹配
					JLabel label = (JLabel) new BasicFinder().find((Container) nodeCmp, new TextMatcher(nameRegexp));				
					if (label != null ) {
						System.out.println(label.getText());
						tmpNodes.add(treeNode.getChildAt(i));	
					}
				} catch (ComponentNotFoundException
						| MultipleComponentsFoundException e) {
					// TODO Auto-generated catch block
				}
			}
		treeNodes.clear();
		treeNodes.addAll(tmpNodes);	
		if (treeNodes.size()==0){
			System.out.println("没有找到对应节点!");
		}
    }
  }
	

